<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends CommonController{
    
    Public function _initialize(){
        $this->base_init('Goods');

    }
    //查看当前耗材
    public function index(){
    	
    	$goodsRes = $this->dbTable->select();
    	//dump($goodsRes);die;
    	
        for ($i=0; $i < count($goodsRes); $i++) {
            $goodsRes[$i]['total'] = $goodsRes[$i]['price']*$goodsRes[$i]['amount'];
            $goodsTotal[]          = $goodsRes[$i]['total'];
        }
        $total = array_sum($goodsTotal);
        $this->assign('goodsList',$goodsRes);
        $this->assign('total',$total);      //总价
        $this->display('goods_list');
    }
    
    //添加耗材信息
    public function addGoods(){
        $date = I();
        $goodsRes = $this->dbTable->add($date);
        if ($goodsRes) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }

    //入库加库存 type=1、领用减库存type=2
    public function updateAmount($gid,$num,$type){

        $goodsTable = M('Goods');
        if ($type == '1') {
            $this->dbTable->where('gid='.$gid)->setInc('amount',"$num");
           
        }else{
            $goodsTable->where('gid='.$gid)->setDec('amount',"$num");
        }
        //echo $goodsTable->getLastsql();

    }


    //查看 所有 耗材信息
    //1 为查看所有上架， 0为所有耗材（包含已上架未上架）
    public function getGoods($status = '1'){
        if ($status == '0') {
            $goodsRes = $this->dbTable
                ->field('gid,rank,goodsname,price,amount,pic')
                ->select();
        }else{
            $where['status'] = '1';
            $goodsRes = $this->dbTable->where($where)
                            ->field('gid,rank,goodsname,price,amount,pic')
                            ->select();
        }
        return $goodsRes;
    }

    //获取 根据商品gid 获取商品信息
    public function getRes($gid = 0){
        
        $goodsRes = $this->dbTable->where('gid='.$gid)->find();
        return $goodsRes;
    }

    //下架耗材
    public function sold_out($gid = 0){
        $where['gid'] = $gid; 
        $date['status'] = '0';
        $res = $this->dbTable->where($where)->setField($date);
        if ($res) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }    

    //上架耗材
    public function putaway($gid = 0){
        $where['gid'] = $gid; 
        $date['status'] = '1';
        $res = $this->dbTable->where($where)->setField($date);
        if ($res) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }


}